package com.wbao.dianniu.tabstrip.fragments;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.listener.IHeadImageListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.GlobalUserData;
import com.wbao.dianniu.update.HeadImageManger;
import com.wbao.dianniu.ui.IndustryAuthActivity;
import com.wbao.dianniu.ui.MyAmActivity;
import com.wbao.dianniu.ui.MyCollectionActivity;
import com.wbao.dianniu.ui.MyQAnswerActivity;
import com.wbao.dianniu.ui.MyQRCodeActivity;
import com.wbao.dianniu.ui.MyScoreActivity;
import com.wbao.dianniu.ui.PerfectInfoActivity;
import com.wbao.dianniu.ui.SettingActivity;
import com.wbao.dianniu.update.ScoreChangeManager;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.PermissionUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

import java.io.File;
import java.util.ArrayList;

/**
 * 我
 *
 * @author yejianjun
 */
public class FourthFragment extends Fragment implements OnClickListener, IHeadImageListener, ScoreChangeManager.IScoreChangeListener {

    /**
     * 头像
     */
    private ImageView headIV;
    /**
     * 个人资料信息
     */
    private RelativeLayout headInfoRl;
    /**
     * 编辑资料
     */
    private Button persionInfoBtn;
    /**
     * 完善资料
     */
    private RelativeLayout prefectInfoRl;
    /**
     * 行业认证
     */
    private Button industryBtn;
    /**
     * 我的问答
     */
    private RelativeLayout rl_myquestion;
    /**
     * 与我相关
     */
    private RelativeLayout rl_anonymous;
    /**
     * 设置
     */
    private RelativeLayout rl_setting;
    /**
     * 我的收藏
     */
    private RelativeLayout rl_mycollection;
    /**
     * 我的积分
     */
    private RelativeLayout rl_score;
    /**
     * 扫一扫
     */
    private RelativeLayout rl_scan;
    private TextView nickNameTV;
    private TextView labelTV;
    private TextView signateTV;
    private Dialog dialog;
    private ImageButton qrCodeBtn;
    private ImageView sexIV;
    private ImageView addV;
    private TextView myScoreTV;
    private HeadImageManger headImageManger;
    private ScoreChangeManager scoreChangeManager;

    public static FourthFragment instance() {
        FourthFragment view = new FourthFragment();
        //在该Fragment的构造函数中注册mTouchListener的回调
        return view;
//        if (null == instance) {
//            instance = new FourthFragment();
//        }
//        //在该Fragment的构造函数中注册mTouchListener的回调
//        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        headImageManger = HeadImageManger.getInstance();
        headImageManger.addListener(this);
        scoreChangeManager = ScoreChangeManager.getInstance();
        scoreChangeManager.addScoreChangeListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (null != headImageManger)
            headImageManger.removeListener(this);
        if (null != scoreChangeManager) {
            scoreChangeManager.removeScoreChangeListener(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fourth_fragment, null);
        initview(view);
        return view;
    }

    private void initview(View view) {
        headIV = (ImageView) view.findViewById(R.id.headIcon);
        headInfoRl = (RelativeLayout) view.findViewById(R.id.head_info_relative);
        persionInfoBtn = (Button) view.findViewById(R.id.persion_info);
        industryBtn = (Button) view.findViewById(R.id.industry_auth);
        prefectInfoRl = (RelativeLayout) view.findViewById(R.id.rl_prefect_info);
        rl_myquestion = (RelativeLayout) view.findViewById(R.id.rl_myquestion);
        rl_anonymous = (RelativeLayout) view.findViewById(R.id.rl_anonymous);
        rl_setting = (RelativeLayout) view.findViewById(R.id.rl_sets);
        rl_mycollection = (RelativeLayout) view.findViewById(R.id.rl_collection);
        rl_score = (RelativeLayout) view.findViewById(R.id.rl_score);
        rl_scan = (RelativeLayout) view.findViewById(R.id.rl_scan);

        myScoreTV = (TextView) view.findViewById(R.id.my_score_tv);
        nickNameTV = (TextView) view.findViewById(R.id.dn_name);
        labelTV = (TextView) view.findViewById(R.id.label_id);
        sexIV = (ImageView) view.findViewById(R.id.user_sex);
        addV = (ImageView) view.findViewById(R.id.add_v_iv);
        signateTV = (TextView) view.findViewById(R.id.dn_signature);
        PictureDownloadUtils.displayHeadImage(getContext(), headIV, GlobalContext.getHeadPic());
        qrCodeBtn = (ImageButton) view.findViewById(R.id.qrcode_Imagebutton);

        nickNameTV.setText(GlobalContext.getNiceName() == null ? "" : GlobalContext.getNiceName());
        labelTV.setText(GlobalContext.getLabel() == null ? "" : GlobalContext.getLabel());
        Utils.showSex(view.getContext(), GlobalContext.getSex(), sexIV);
        if (GlobalContext.getIndustryAuthority()) {
            addV.setVisibility(View.VISIBLE);
        } else {
            addV.setVisibility(View.GONE);
        }
        signateTV.setText(GlobalContext.getSignate() == null ? "简介：暂无" : "简介：" + GlobalContext.getSignate());
        if (GlobalContext.getPerfectData()) {
            prefectInfoRl.setVisibility(View.GONE);
        } else {
            prefectInfoRl.setVisibility(View.VISIBLE);
        }
        GlobalUserData userData = new GlobalUserData();
        myScoreTV.setText(userData.getScore() + "");

        headIV.setOnClickListener(this);
        headInfoRl.setOnClickListener(this);
        persionInfoBtn.setOnClickListener(this);
        industryBtn.setOnClickListener(this);
        prefectInfoRl.setOnClickListener(this);
        rl_myquestion.setOnClickListener(this);
        rl_anonymous.setOnClickListener(this);
        rl_setting.setOnClickListener(this);
        rl_mycollection.setOnClickListener(this);
        rl_score.setOnClickListener(this);
        rl_scan.setOnClickListener(this);
        qrCodeBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headIcon:
                /** 点击放大图片 */
                ArrayList<String> urls2 = new ArrayList<>();
                urls2.add(GlobalContext.cdndownUrl + GlobalContext.getHeadPic());
                Utils.imageBrower(getContext(), 0, urls2);
                break;
            case R.id.persion_info:
            case R.id.head_info_relative:
                /** 跳转到完善资料 */
                intent(PerfectInfoActivity.class);
                break;
            case R.id.industry_auth:
                if (!GlobalContext.getPerfectData()) {
                    ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                } else {
                    /** 跳转到行业认证 */
                    intent(IndustryAuthActivity.class);
                }
                break;
            case R.id.rl_prefect_info:
                /** 跳转到完善资料*/
                intent(PerfectInfoActivity.class);
                break;
            case R.id.rl_myquestion:
                /** 跳转到我的问答 */
                intent(MyQAnswerActivity.class);
                break;
            case R.id.rl_anonymous:
                /** 跳转到匿名八卦 */
                intent(MyAmActivity.class);
                break;
            case R.id.rl_collection:
                /** 跳转到我的收藏 */
                intent(MyCollectionActivity.class);
                break;
            case R.id.rl_score:
                intent(MyScoreActivity.class);
                break;
            case R.id.rl_scan:
                if (!GlobalContext.getPerfectData()) {
                    ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                    return;
                }
                // 扫一扫
                PermissionUtils.getInstance().intentToScan(getActivity());
                break;
            case R.id.rl_sets:
                /** 跳转到设置 */
                intent(SettingActivity.class);
                break;
            case R.id.qrcode_Imagebutton:
                if (!GlobalContext.getPerfectData()) {
                    ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                } else {
                    /**    跳转到二维码界面	*/
                    intent(MyQRCodeActivity.class);
                }
                break;
            default:
                break;
        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void intent(Class cls) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    @Override
    public void imageChange(String headCachePath) {
        Glide.with(this)
                .load(new File(headCachePath))
                .centerCrop()
                .placeholder(R.drawable.default_head_icon)
                .crossFade()
                .override(Utils.dip2px(getContext(), 40), Utils.dip2px(getContext(), 40))
                .bitmapTransform(new GlideCircleTransform(getContext()))
                .thumbnail(0.1f)
                .into(headIV);
    }

    @Override
    public void dataChanged(String nickName, String sex, String signate,
                            String label) {
        prefectInfoRl.setVisibility(View.GONE);
        GlobalContext.setLabel(label);
        labelTV.setText(label);
        Integer sexFlag = null;
        if (sex.equals("男")) {
            sexFlag = 1;
        } else if (sex.equals("女")) {
            sexFlag = 0;
        }
        Utils.showSex(getContext(), sexFlag, sexIV);
        signateTV.setText("简介：" + signate);
        nickNameTV.setText(nickName);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("FourthFragment"); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("FourthFragment");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        /**    注释这行代码，防止界面跳转时，界面被回收	*/
        super.onSaveInstanceState(outState);
    }

    @Override
    public void updateScore() {
        try {
            GlobalUserData userData = new GlobalUserData();
            myScoreTV.setText(userData.getScore() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}